vim9script

# Immersive mode function
export def Immersive()
	# Check if writer mode is on, by fetching the bacground color of the
	# separator (15 = white) and, if it's on, turns it off
	if matchstr(execute('hi VertSplit'), 'ctermfg=\zs\d\d') == '15'
		hi VertSplit ctermfg=black
		hi NonText ctermfg=lightblue
		set laststatus=1
		set rnu
		only
		return
	endif

	# Formating
	hi VertSplit ctermfg=white # Hides split separtor
	hi NonText ctermfg=white # Hides blank line character
	set laststatus=0 # Hides status line
	set nornu # Turn off relative numbers

	var Resize_window = (new: number) => {
		var curr = winwidth(0)
		var op = (curr < new) ? '>' : '<'
		feedkeys(abs(new - curr) .. "\<C-w>" .. op, 'x')
	}
	
	# Make two read-only buffers to act as margins
	const margin_size = (winwidth(0) - &tw) / 2
	var mod = (margin_size % 2 == 0) ?  0 : 1
	
	vnew | set nomodifiable | vnew | set nomodifiable
	Resize_window(margin_size)
	wincmd l
	Resize_window(margin_size + mod - 2)
	wincmd l
	wincmd R
enddef
